﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PPI.Client.Utils
{
    public class GeoHelper
    {
        public static double GetDistanceBetweenCoordinates(double firstLatitude, double firstLongitude, double secondLatitude, double secondLongitude)
        {
            double deltaLongitude = ConvertDegreesToRadians(secondLongitude - firstLongitude);
            double deltaLatitude = ConvertDegreesToRadians(secondLatitude - firstLatitude);
            double earthRadius = 6371;

            double a = (Math.Sin(deltaLatitude/2)*Math.Sin(deltaLatitude/2)) +
                       (Math.Cos(ConvertDegreesToRadians(firstLatitude)) + Math.Cos(ConvertDegreesToRadians(secondLatitude))) *
                       (Math.Sin(deltaLongitude / 2) * Math.Sin(deltaLongitude / 2));
            double c = 2*Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double distance = c*earthRadius;
            // return in meters
            return distance*1000;
        }
        
        public static double ConvertDegreesToRadians(double degrees)
        {
            return (Math.PI / 180) * degrees;
        }  
    }
}
